//
//  TJretweeteView.swift
//  TJSinaWeibo
//
//  Created by 唐后能 on 16/4/15.
//  Copyright © 2016年 itheima. All rights reserved.
//

import UIKit

class TJRetweeteView: UIView {

    // 被转发的微博模型属性.
    var retweeteStatus:TJStatus? {
    
        didSet {
        
            // 给内容Label赋值.
            let screen_name = retweeteStatus?.user?.screen_name ?? "被转发微博的用户名" //用户名.
            let content = retweeteStatus?.text ?? "被转发微博的内容为空" // 微博内容.
            let retweetString = "@\(screen_name):" + "\(content)"
            // 将 retweetString 转成 attString
            retweeteContentLabel.attributedText = retweetString.emoticonNameTextToPicAttText()
            
            // 被转发的视图赋值.
            retweetePictureView.picURLS = retweeteStatus?.picURLS
            // 当没有配图的时候,更新配图的顶部约束.
            retweetePictureView.snp_updateConstraints { (make) -> Void in
                let margin = retweeteStatus?.picURLS == nil ? 0 : TJStatusCellMargin
                make.top.equalTo(retweeteContentLabel.snp_bottom).offset(margin)
            }
        }
    }
    
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    private func setupUI() {
    
        // 1,添加子控件.
        addSubview(bgButton)
        addSubview(retweeteContentLabel)
        addSubview(retweetePictureView)
        
        // 2,添加约束.
        // 背景.
        bgButton.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(self)
        }
        // 内容label.
        retweeteContentLabel.snp_makeConstraints { (make) -> Void in
            make.leading.top.equalTo(self).offset(TJStatusCellMargin)
            make.trailing.equalTo(self).offset(-TJStatusCellMargin)
        }
        // 配图. 
        retweetePictureView.snp_makeConstraints { (make) -> Void in
        
            make.leading.equalTo(self).offset(TJStatusCellMargin)
            make.top.equalTo(retweeteContentLabel.snp_bottom).offset(TJStatusCellMargin)
            make.size.equalTo(CGSizeZero)
        }
        // 给自己设置高度底部约束,参照最底部的子控件,来自适应约束.
        self.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(retweetePictureView).offset(TJStatusCellMargin)
        }
        
    }

    
    // MARK - 懒加载.
    // 背景按钮.
    private lazy var bgButton:UIButton = {
    
        let btn = UIButton()
        // 设置.
        btn.setBackgroundImage(UIImage(named: "retweet_background"), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "retweet_background_highlighted"), forState: UIControlState.Highlighted)
        return btn
    }()
    
    // 被转发的内容.
    private lazy var retweeteContentLabel:FFLabel = {
    
        let label = FFLabel()
        // 设置.
        label.numberOfLines = 0
        label.font = UIFont.systemFontOfSize(CGFloat(TJStatusContentSize))
        label.textColor = UIColor.grayColor()
        // 设置FFLabel的代理.
        label.labelDelegate = self
        // 设置label宽度,否则行高不对.
        label.preferredMaxLayoutWidth = UIScreen.mainScreen().bounds.width - 2 * TJStatusCellMargin
        return label
    }()
    
    // 被转发的配图.
    private lazy var retweetePictureView:TJPictureView = TJPictureView()
}
//
extension TJRetweeteView:FFLabelDelegate {

    func labelDidSelectedLinkText(label: FFLabel, text: String) {
        TJPrint("\(text)")
    }
}
